<template>
  <div>
    <search :formItems='searchItems' @search='getList' ref='getCheckInSearch' />
    <el-button type="primary" class="marginBottom10" @click="isAdd = true; showAddBox = true; cleanForm()">新增规则</el-button>
    <el-table :data='tableData' style='width: 100%'>
      <el-table-column type="index" label='序号' ></el-table-column>
      <el-table-column prop='createtime' sortable label='创建时间' ></el-table-column>
      <el-table-column prop='n' label='连续签到天数'></el-table-column>
      <el-table-column prop='m' label='获得抽奖机会数'></el-table-column>
      <el-table-column prop='content' label='提醒文本'></el-table-column>
      <el-table-column label='操作'>
        <template slot-scope="scope">
          <span class="astyle"  @click="editItem(scope.row)">编辑</span>
          <span class="dstyle"  @click="deleteItem(1, scope.row.id)">删除</span>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.page"
        :page-sizes="[20, 50, 100]"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
    <el-dialog
      :close-on-click-modal="false"
      :title="isAdd ? '新增' : '编辑'"
      :visible.sync="showAddBox"
      >
      <el-form :model="diaForm"  ref="getCheckInForm" label-width="110px">
        <el-form-item label="连续签到次数" prop="n">
          <el-input-number v-model="diaForm.n"/>
        </el-form-item>

        <el-form-item label="奖励抽奖次数" prop="m">
          <el-input-number v-model="diaForm.m"/>
        </el-form-item>

        <el-form-item label="提醒文本" prop="content">
          <el-input v-model="diaForm.content" type="textarea">
          </el-input>
        </el-form-item>

      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="showAddBox = false">取 消</el-button>
        <el-button type="primary" @click="handleClickSendForm('getCheckInForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: 'GetCheckIn',
  data () {
    return {
      searchItems: [
        // {
        //   label: '连续签到天数',
        //   width: '100',
        //   type: 'input',
        //   key: 'n'
        // },
        // {
        //   label: '获得次数',
        //   width: '100',
        //   type: 'input',
        //   key: 'luckdrawcount'
        // }
      ],
      isAdd: false,
      showAddBox: false,
      diaForm: {
        id: '',
        n: 1,
        m: 1,
        content: ''
      },
      tableData: [],
      page: {
        page: 1,
        size: 20,
        total: 1
      }
    }
  },
  methods: {
    getList () {
      let searchData = {}
      if (this.$refs['getCheckInSearch']) {
        searchData = this.$refs['getCheckInSearch'].search()
      }
      let params = {
        ...searchData,
        page: this.page.page,
        size: this.page.size
      }
      this.axios.get('/Luckdraw/signrewardindex', { params }).then(res => {
        if (res.data.status === 1 || res.data.status === '1') {
          this.page.total = Number(res.data.data.count)
          this.tableData = res.data.data.list
        }
      })
    },
    handleSizeChange (val) {
      this.page.size = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.page.page = val
      this.getList()
    },
    editItem (row) {
      let self = this
      for (let key in this.diaForm) {
        self.diaForm[key] = row[key]
      }
      this.isAdd = false
      this.showAddBox = true
    },
    deleteItem (e = 1, val) {
      let self = this
      let params = {}
      params.id = val
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.axios.get('/Luckdraw/delsignreward', { params: {id: val} }).then(res => {
          if (res.data.status === 1 || res.data.status === '1') {
            self.$message({
              message: '删除成功',
              type: 'success'
            })
            self.getList()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    cleanForm () {
      this.diaForm.n = 1
      this.diaForm.m = 1
      this.diaForm.id = ''
      this.diaForm.content = ''
    },
    changeNumber (ele, val) {
      if (this.diaForm[ele] + val > -1) {
        this.diaForm[ele] += val
      }
    },
    handleClickSendForm (formName) {
      let url = this.isAdd ? '/Luckdraw/addsignreward' : '/Luckdraw/editsignreward'
      let self = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = {}
          for (let key in self.diaForm) {
            if (key !== 'id') {
              if (self.diaForm[key] !== '') {
                params[key] = self.diaForm[key]
              }
            }
          }
          if (!self.isAdd) {
            params['id'] = self.diaForm.id
          }
          this.axios.post(url, self.qs.stringify(params)).then(res => {
            if (res.data.status === 1 || res.data.status === '1') {
              self.$message({
                message: '成功',
                type: 'success'
              })
              self.showAddBox = false
              self.getList()
            }
          })
        } else {
          return false
        }
      })
    }
  },
  mounted () {
    this.getList()
  }
}
</script>
<style scoped>
@import "../../../assets/css/style.css";
</style>
